
projectPath=`pwd`

#配置， 在linux下运行
imageName="platform"
#read -p "请输入镜像名称(默认pytorch_dev)：" imageName
if [ "${imageName}" = "" ];then
		imageName="pytorch_dev" 
fi
tag="v1"
#read -p "请输入镜像tag(默认latest)：" tag
if [ "${tag}" = "" ];then
		tag="latest"
fi
containerName=${imageName}${tag}
echo containerName: ${containerName}

imageFile='pytorch_dev.latest.tar'		#镜像文件名
echo "默认配置本地镜像文件：${imageFile}"
imgSaveMount="/home/recvImg"	#接收图片目录
echo "挂载图片存储的路径, 默认：${imgSaveMount}"

#判断该路径是否存在
if [ -d "${imgSaveMount}" ];then
	echo "${imgSaveMount}"存在
else
	mkdir -p ${imgSaveMount}
	chmod 777 ${imgSaveMount}
	echo "创建挂载目录"
fi

##配置结束


isContainerName=`docker ps -a |grep ${containerName}`
isExit=`docker ps -a |grep ${containerName} |grep 'Exited'`
isImage=`docker images |grep ${imageName} |grep ${tag}`
echo ${isImage}
imageName=${imageName}:${tag}
echo imageName: ${imageName}

if [ "${isContainerName}" = "" ];then
	#容器不存在
	echo '容器不存在, 检测镜像'
	if [ "${isImage}" = "" ];then
		#image不存在
		echo '镜像不存在'
		if [ -f "./${imageFile}" ];then
			echo "镜像文件存在， 开始导入镜像"
			#docker import - ${imageName} < ${imageFile}
			docker load < ${imageFile}
			echo "导入镜像成功, 镜像：" ${imageName}
			
		else
			echo "镜像文件不存在，请检查镜像文件" ${imageFile}
			exit 0
		fi
	else
		echo "镜像存在"
	fi
	#image存在, 创建容器
	echo "创建新容器：" ${containerName}
	#docker run -it --rm --name ${containerName}  -v ${projectPath}/:/home/workSpace -v /${imgSaveMount}/:${imgSaveMount}/ ${imageName} /bin/bash -c "cd /home/workSpace/ji_fang_demo/ && /bin/bash"  &&
	# --gpus all
	docker run -it --rm  --name ${containerName}  -v ${projectPath}/:/home/workspace ${imageName} /bin/bash  &&
	echo "end ..."
	
else 
	if [ "${isExit}" = "" ];then
		#容器存在, 正在运行
		echo "容器存在, 正在运行, 重启"
		docker restart ${containerName}
		
		
	else  
		#容器存在, 已停止
		echo "容器存在, 已停止, 打开"
		docker start ${containerName}
	fi
	
fi



#docker run -it --rm --name ${containerName}  -v ${projectPath}/:/home/workSpace ${imageName} /bin/bash -C "/home/workSpace/containerAutoStart.sh"

#sudo apt update
#pip install pyyaml
#pip install matplotlib
#pip install seaborn





